<template>
  <div>
    <div class="search_bar">
      <el-input placeholder="请输入关键字" v-model="keyword"></el-input>
      <el-button @click="dialogFormVisible = true">添加</el-button>
    </div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column fixed prop="create_time" label="日期" width="150">
        <!-- 表格嵌套自定义内容 -->
        <template slot-scope="scope">
          <!-- 过滤器 -->
          {{ scope.row['create_time'] | timeToDate }}
        </template>
      </el-table-column>
      <el-table-column prop="title" label="标题" width="120"></el-table-column>
      <el-table-column prop="content" label="内容" width="120"></el-table-column>
      <el-table-column prop="author" label="作者" width="120"></el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="editBlogFun(scope.row)" type="text" size="small">编辑</el-button>
          <el-button @click="delBlogFun(scope.row.id)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="footer_bar">
      <el-pagination
        background
        layout="prev, pager, next"
        :current-page="pageIndex"
        :page-size="pageSize"
        :total="total"
        @current-change="pageChange"
      ></el-pagination>
    </div>
    <!-- 添加弹框 -->
    <el-dialog title="添加博客" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="博客标题" :label-width="formLabelWidth">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="博客内容" :label-width="formLabelWidth">
          <el-input v-model="form.content" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="博客作者" :label-width="formLabelWidth">
          <el-input v-model="form.author" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addBlogFun">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  filters: {
    // 过滤器
    timeToDate(value) {
      return new Date(value).toLocaleDateString()
    }
  },
  data() {
    return {
      tableData: [],
      total: 0,
      keyword: "",
      timer: null,
      pageSize: 2,
      pageIndex: 1,
      dialogFormVisible: false,
      form: {
        title: "",
        content: "",
        author: ""
      },
      formLabelWidth: '120px'
    }
  },
  watch: {
    keyword() {
      // 怎么做函数防抖呢？
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.pageChange()
      }, 800)
    },
    dialogFormVisible() {
      if (this.dialogFormVisible === false) {
        this.form = {
          title: "",
          content: "",
          author: ""
        }
      }
    }
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      // 请求数据
      this.pageChange()
    },
    // 分页
    pageChange(index = 1) {
      this.pageIndex = index;
      this.$api.getBlog({ pageIndex: this.pageIndex, pageSize: this.pageSize, keyword: this.keyword }).then(res => {
        if (res.success) {
          this.tableData = res.data.data;
          this.total = res.data.total;
        }
      })
    },
    //添加博客
    addBlogFun() {
      // 判断是添加还是编辑
      if (this.form.id) {
        this.$api.editBlog({ ...this.form }).then(res => {
          if (res.success) {
            this.$message.success(res.message);
            this.dialogFormVisible = false; // 添加成功后关闭弹框
            this.pageChange(); // 重新请求一遍列表拿到新数据
          } else {
            this.$message.error(res.message);
          }
        })
      } else {
        this.$api.addBlog({ ...this.form }).then(res => {
          if (res.success) {
            this.$message.success(res.message);
            this.dialogFormVisible = false; // 添加成功后关闭弹框
            this.pageChange(); // 重新请求一遍列表拿到新数据
          } else {
            this.$message.error(res.message);
          }
        })
      }

    },
    //删除博客
    delBlogFun(id) {
      this.$api.delBlog(id).then(res => {
        if (res.success) {
          this.$message.success(res.message);
          this.pageChange(); // 重新请求一遍列表拿到新数据
        } else {
          this.$message.error(res.message);
        }
      })
    },
    // 编辑博客
    editBlogFun(row) {
      this.form = row;
      this.dialogFormVisible = true;
    }
  },


}
</script>

<style>
</style>